package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.utils.BeanUtils;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.entity.*;
import com.zatech.cgnci.project.mapper.CgnciReinsuranceInfoMapper;
import com.zatech.cgnci.project.service.*;
import com.zatech.cgnci.project.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 分入分出信息表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2024-12-17
 */
@Service
public class CgnciReinsuranceInfoServiceImpl extends ServiceImpl<CgnciReinsuranceInfoMapper, CgnciReinsuranceInfoDO> implements CgnciReinsuranceInfoService {
    @Resource
    private CgnciReinsuranceRiskService cgnciReinsuranceRiskService;
    @Resource
    private CgnciReinsuranceRiskInsuredService cgnciReinsuranceRiskInsuredService;
    @Resource
    private CgnciReinsuranceRiskLayerService cgnciReinsuranceRiskLayerService;
    @Resource
    CgnciReinsurerService reinsurerService;
    @Resource
    CgnciAccessoryService accessoryService;


    @Override
    public CgnciReinsuranceInfoVO getReinsuranceInfo(Long insuranceId) {
        CgnciReinsuranceInfoDO divideInDO = this.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, insuranceId).eq(CgnciReinsuranceInfoDO::getReinsuranceType, 1));
        if (VerifyUtils.isNotEmpty(divideInDO)) {
            CgnciReinsuranceInfoVO divideInVO = ConvertUtils.convert(divideInDO, CgnciReinsuranceInfoVO.class);
            // 分入险种
            List<CgnciReinsuranceRiskDO> divideInRiskDOList = cgnciReinsuranceRiskService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, divideInDO.getReinsuranceId()));
            List<CgnciReinsuranceRiskVO> divideInRiskVOList = ConvertUtils.convert(divideInRiskDOList, CgnciReinsuranceRiskVO.class);
            divideInVO.setRisks(divideInRiskVOList);
            return divideInVO;
        }
        return new CgnciReinsuranceInfoVO();
    }

    @Override
    public CgnciReinsuranceInfoProjectVO getDivideInReinsuranceByProjectId(Long projectId) {
        // 查询分入信息
        CgnciReinsuranceInfoDO divideInDO = this.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, projectId)
                .eq(CgnciReinsuranceInfoDO::getReinsuranceType, 1));
        if (VerifyUtils.isNotEmpty(divideInDO)) {
            CgnciReinsuranceInfoProjectVO divideInVO = ConvertUtils.convert(divideInDO, CgnciReinsuranceInfoProjectVO.class);
            // 查询分入险种
            List<CgnciReinsuranceRiskDO> divideInRiskDOList = cgnciReinsuranceRiskService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, divideInDO.getReinsuranceId()));
            if (VerifyUtils.isNotEmpty(divideInRiskDOList)) {
                List<CgnciReinsuranceRiskProjectVO> divideInRiskVOList = ConvertUtils.convert(divideInRiskDOList, CgnciReinsuranceRiskProjectVO.class);
                List<Long> reinsuranceRiskIds = divideInRiskVOList.stream().map(CgnciReinsuranceRiskProjectVO::getReinsuranceRiskId).collect(Collectors.toList());

                Map<Long, List<CgnciReinsuranceRiskInsuredProjectVO>> riskIdMap = new HashMap<>();
                Map<Long, List<CgnciReinsuranceRiskLayerProjectVO>> riskInsuredIdMap = new HashMap<>();
                Map<Long, List<CgnciAccessoryVO>> riskLayerIdMap = new HashMap<>();

                // 查询分入险种标的
                List<CgnciReinsuranceRiskInsuredDO> insuredDOList = cgnciReinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                        .in(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskIds));
                if (VerifyUtils.isNotEmpty(insuredDOList)) {
                    List<CgnciReinsuranceRiskInsuredProjectVO> insuredVOList = ConvertUtils.convert(insuredDOList, CgnciReinsuranceRiskInsuredProjectVO.class);
                    // 分人险种标的ID集合
                    List<Long> riskInsuredIds = insuredVOList.stream().map(CgnciReinsuranceRiskInsuredProjectVO::getRiskInsuredId).collect(Collectors.toList());
                    // 分入险种标的根据险种id分组
                    riskIdMap = insuredVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskInsuredProjectVO::getReinsuranceRiskId));

                    // 分入险种层
                    List<CgnciReinsuranceRiskLayerDO> divideInRiskLayerDOList = cgnciReinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                            .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, riskInsuredIds));
                    if (VerifyUtils.isNotEmpty(divideInRiskLayerDOList)) {
                        List<CgnciReinsuranceRiskLayerProjectVO> divideInRiskLayerVOList = ConvertUtils.convert(divideInRiskLayerDOList, CgnciReinsuranceRiskLayerProjectVO.class);
                        // ID集合
                        List<Long> riskLayerIds = divideInRiskLayerVOList.stream().map(CgnciReinsuranceRiskLayerProjectVO::getLayerId).collect(Collectors.toList());
                        // 分入险种层根据分入险种标的id分组
                        riskInsuredIdMap = divideInRiskLayerVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskLayerProjectVO::getReinsuranceRiskId));

                        // 查询免赔附件
                        List<CgnciAccessoryDO> accessoryDOList = accessoryService.list(new LambdaQueryWrapper<CgnciAccessoryDO>()
                                .in(CgnciAccessoryDO::getBusinessId, riskLayerIds));
                        if (VerifyUtils.isNotEmpty(accessoryDOList)) {
                            List<CgnciAccessoryVO> accessoryVOList = ConvertUtils.convert(accessoryDOList, CgnciAccessoryVO.class);
                            // 分入险种层根据分入险种标的id分组
                            riskLayerIdMap = accessoryVOList.stream().collect(Collectors.groupingBy(CgnciAccessoryVO::getBusinessId));
                        }
                    }
                }

                // 组装VO
                Map<Long, List<CgnciReinsuranceRiskInsuredProjectVO>> finalRiskIdMap = riskIdMap;
                Map<Long, List<CgnciReinsuranceRiskLayerProjectVO>> finalRiskInsuredIdMap = riskInsuredIdMap;
                Map<Long, List<CgnciAccessoryVO>> finalRiskLayerIdMap = riskLayerIdMap;
                divideInRiskVOList.forEach(risk -> {
                    List<CgnciReinsuranceRiskInsuredProjectVO> riskInsuredVOS = finalRiskIdMap.get(risk.getReinsuranceRiskId());
                    if (VerifyUtils.isNotEmpty(riskInsuredVOS)) {
                        riskInsuredVOS.forEach(riskInsured -> {
                            List<CgnciReinsuranceRiskLayerProjectVO> riskLayerVOS = finalRiskInsuredIdMap.get(riskInsured.getRiskInsuredId());
                            if (VerifyUtils.isNotEmpty(riskLayerVOS)) {
                                riskLayerVOS.forEach(riskLayer -> {
                                    List<CgnciAccessoryVO> accessoryVOList = finalRiskLayerIdMap.get(riskLayer.getLayerId());
                                    riskLayer.setAccessoryVOList(accessoryVOList);
                                });
                                riskInsured.setRiskLayers(riskLayerVOS);
                            }
                        });
                    }
                    risk.setRiskInsureds(riskInsuredVOS);
                });
                divideInVO.setRisks(divideInRiskVOList);
            }
            return divideInVO;
        }
        return null;
    }

    @Override
    public List<CgnciReinsuranceInfoProjectVO> listHyDivideOutReinsuranceByProjectId(Long projectId) {
        List<CgnciReinsuranceInfoDO> hyDivideOutDOList = this.list(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, projectId)
                .eq(CgnciReinsuranceInfoDO::getReinsuranceType, 2));
        if (VerifyUtils.isEmpty(hyDivideOutDOList)) {
            return null;
        }
        List<CgnciReinsuranceInfoProjectVO> hyDivideOutVOList = ConvertUtils.convert(hyDivideOutDOList, CgnciReinsuranceInfoProjectVO.class);
        List<Long> reinsuranceIds = hyDivideOutVOList.stream().map(CgnciReinsuranceInfoProjectVO::getReinsuranceId).collect(Collectors.toList());
        Map<Long, List<CgnciReinsuranceRiskProjectVO>> reinsuranceIdMap = new HashMap<>();
        Map<Long, List<CgnciReinsuranceRiskInsuredProjectVO>> reinsuranceRiskIdMap = new HashMap<>();
        Map<Long, List<CgnciReinsuranceRiskLayerProjectVO>> reinsuranceRiskInsuredIdMap = new HashMap<>();

        // 查询险种
        List<CgnciReinsuranceRiskDO> hyDivideOutRiskDOList = cgnciReinsuranceRiskService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                .in(CgnciReinsuranceRiskDO::getReinsuranceId, reinsuranceIds));
        if (VerifyUtils.isNotEmpty(hyDivideOutRiskDOList)) {
            List<CgnciReinsuranceRiskProjectVO> hyDivideOutRiskRiskVOList = ConvertUtils.convert(hyDivideOutRiskDOList, CgnciReinsuranceRiskProjectVO.class);
            List<Long> reinsuranceRiskIds = hyDivideOutRiskRiskVOList.stream().map(CgnciReinsuranceRiskProjectVO::getReinsuranceRiskId).collect(Collectors.toList());
            // 险种根据合约项分组
            reinsuranceIdMap = hyDivideOutRiskRiskVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskProjectVO::getReinsuranceId));

            // 查询险种标的
            List<CgnciReinsuranceRiskInsuredDO> insuredDOList = cgnciReinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                    .in(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskIds));
            if (VerifyUtils.isNotEmpty(insuredDOList)) {
                // 险种标的VO
                List<CgnciReinsuranceRiskInsuredProjectVO> insuredVOList = ConvertUtils.convert(insuredDOList, CgnciReinsuranceRiskInsuredProjectVO.class);
                // 险种标的ID集合
                List<Long> riskInsuredIds = insuredVOList.stream().map(CgnciReinsuranceRiskInsuredProjectVO::getRiskInsuredId).collect(Collectors.toList());
                // 险种标的根据险种分组
                reinsuranceRiskIdMap = insuredVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskInsuredProjectVO::getReinsuranceRiskId));

                // 查询险种层
                List<CgnciReinsuranceRiskLayerDO> divideInRiskLayerDOList = cgnciReinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                        .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, riskInsuredIds));
                if (VerifyUtils.isNotEmpty(divideInRiskLayerDOList)) {
                    List<CgnciReinsuranceRiskLayerProjectVO> divideInRiskLayerVOList = ConvertUtils.convert(divideInRiskLayerDOList, CgnciReinsuranceRiskLayerProjectVO.class);
                    // 险种层根据险种标的分组
                    reinsuranceRiskInsuredIdMap = divideInRiskLayerVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskLayerProjectVO::getReinsuranceRiskId));
                }
            }
        }

        // 组装VO
        Map<Long, List<CgnciReinsuranceRiskProjectVO>> finalReinsuranceIdMap = reinsuranceIdMap;
        Map<Long, List<CgnciReinsuranceRiskInsuredProjectVO>> finalReinsuranceRiskIdMap = reinsuranceRiskIdMap;
        Map<Long, List<CgnciReinsuranceRiskLayerProjectVO>> finalReinsuranceRiskInsuredIdMap = reinsuranceRiskInsuredIdMap;
        hyDivideOutVOList.forEach(hyDivideOutVO -> {
            List<CgnciReinsuranceRiskProjectVO> hyDivideOutRiskRiskVOList = finalReinsuranceIdMap.get(hyDivideOutVO.getReinsuranceId());
            // 分出险种
            hyDivideOutRiskRiskVOList.forEach(risk -> {
                List<CgnciReinsuranceRiskInsuredProjectVO> RiskInsuredVOS = finalReinsuranceRiskIdMap.get(risk.getReinsuranceRiskId());
                if (VerifyUtils.isNotEmpty(RiskInsuredVOS)) {
                    RiskInsuredVOS.forEach(riskInsuredVO -> {
                        List<CgnciReinsuranceRiskLayerProjectVO> riskLayerVOS = finalReinsuranceRiskInsuredIdMap.get(riskInsuredVO.getRiskInsuredId());
                        riskInsuredVO.setRiskLayers(riskLayerVOS);
                    });
                }
                risk.setRiskInsureds(RiskInsuredVOS);
            });
            hyDivideOutVO.setRisks(hyDivideOutRiskRiskVOList);
        });
        return hyDivideOutVOList;

    }

    @Override
    public CgnciReinsuranceInfoProjectVO getLfDivideOutReinsuranceByProjectId(Long projectId) {
        CgnciReinsuranceInfoDO lfDivideOutDO = this.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, projectId).eq(CgnciReinsuranceInfoDO::getReinsuranceType, 3));
        if (VerifyUtils.isNotEmpty(lfDivideOutDO)) {
            CgnciReinsuranceInfoProjectVO lfDivideOutVO = ConvertUtils.convert(lfDivideOutDO, CgnciReinsuranceInfoProjectVO.class);
            // 查询险种
            List<CgnciReinsuranceRiskDO> lfDivideOutRiskDOList = cgnciReinsuranceRiskService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, lfDivideOutDO.getReinsuranceId()));
            if (VerifyUtils.isNotEmpty(lfDivideOutRiskDOList)) {
                List<CgnciReinsuranceRiskProjectVO> lfDivideOutRiskRiskVOList = ConvertUtils.convert(lfDivideOutRiskDOList, CgnciReinsuranceRiskProjectVO.class);
                // 险种ID集合
                List<Long> reinsuranceRiskIds = lfDivideOutRiskRiskVOList.stream().map(CgnciReinsuranceRiskProjectVO::getReinsuranceRiskId).collect(Collectors.toList());

                Map<Long, List<CgnciReinsuranceRiskInsuredProjectVO>> reinsuranceRiskIdMap = new HashMap<>();
                Map<Long, List<CgnciReinsuranceRiskLayerProjectVO>> reinsuranceRiskInsuredIdMap = new HashMap<>();

                // 查询险种标的
                List<CgnciReinsuranceRiskInsuredDO> lfDivideOutRiskInsuredDOList = cgnciReinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                        .in(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskIds));
                if (VerifyUtils.isNotEmpty(lfDivideOutRiskInsuredDOList)) {
                    List<CgnciReinsuranceRiskInsuredProjectVO> lfDivideOutRiskInsuredVOList = ConvertUtils.convert(lfDivideOutRiskInsuredDOList, CgnciReinsuranceRiskInsuredProjectVO.class);
                    // 分人险种标的ID集合
                    List<Long> riskInsuredIds = lfDivideOutRiskInsuredVOList.stream().map(CgnciReinsuranceRiskInsuredProjectVO::getRiskInsuredId).collect(Collectors.toList());
                    // 分入险种标的根据险种id分组
                    reinsuranceRiskIdMap = lfDivideOutRiskInsuredVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskInsuredProjectVO::getReinsuranceRiskId));

                    // 查询险种层
                    List<CgnciReinsuranceRiskLayerDO> lfDivideOutRiskLayerDOList = cgnciReinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                            .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, riskInsuredIds));
                    if (VerifyUtils.isNotEmpty(lfDivideOutRiskLayerDOList)) {
                        List<CgnciReinsuranceRiskLayerProjectVO> lfDivideOutRiskLayerVOList = ConvertUtils.convert(lfDivideOutRiskLayerDOList, CgnciReinsuranceRiskLayerProjectVO.class);
                        // 分入险种层根据险种标的分组
                        reinsuranceRiskInsuredIdMap = lfDivideOutRiskLayerVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskLayerProjectVO::getReinsuranceRiskId));
                    }
                }

                // 组装VO
                Map<Long, List<CgnciReinsuranceRiskInsuredProjectVO>> finalReinsuranceRiskIdMap = reinsuranceRiskIdMap;
                Map<Long, List<CgnciReinsuranceRiskLayerProjectVO>> finalReinsuranceRiskInsuredIdMap = reinsuranceRiskInsuredIdMap;
                lfDivideOutRiskRiskVOList.forEach(risk -> {
                    if (VerifyUtils.isNotEmpty(finalReinsuranceRiskIdMap)) {
                        List<CgnciReinsuranceRiskInsuredProjectVO> riskInsuredVOS = finalReinsuranceRiskIdMap.get(risk.getReinsuranceRiskId());
                        if (VerifyUtils.isNotEmpty(riskInsuredVOS)) {
                            riskInsuredVOS.forEach(riskInsured -> {
                                if (VerifyUtils.isNotEmpty(finalReinsuranceRiskInsuredIdMap)) {
                                    List<CgnciReinsuranceRiskLayerProjectVO> riskLayerVOS = finalReinsuranceRiskInsuredIdMap.get(riskInsured.getRiskInsuredId());
                                    riskInsured.setRiskLayers(riskLayerVOS);
                                }
                            });
                            risk.setRiskInsureds(riskInsuredVOS);
                        }
                    }
                });
                lfDivideOutVO.setRisks(lfDivideOutRiskRiskVOList);
            }
            return lfDivideOutVO;
        }
        return null;
    }

    @Override
    public CgnciReinsuranceInfoVO getDivideInReinsuranceByInsuranceId(Long insuranceId) {
        CgnciReinsuranceInfoDO divideInDO = this.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, insuranceId).eq(CgnciReinsuranceInfoDO::getReinsuranceType, 1));
        if (VerifyUtils.isNotEmpty(divideInDO)) {
            CgnciReinsuranceInfoVO divideInVO = ConvertUtils.convert(divideInDO, CgnciReinsuranceInfoVO.class);
            // 分入险种
            List<CgnciReinsuranceRiskDO> divideInRiskDOList = cgnciReinsuranceRiskService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, divideInDO.getReinsuranceId()));
            if (VerifyUtils.isNotEmpty(divideInRiskDOList)) {
                List<CgnciReinsuranceRiskVO> divideInRiskVOList = ConvertUtils.convert(divideInRiskDOList, CgnciReinsuranceRiskVO.class);
                List<Long> reinsuranceRiskIds = divideInRiskVOList.stream().map(CgnciReinsuranceRiskVO::getReinsuranceRiskId).collect(Collectors.toList());

                Map<Long, List<CgnciReinsuranceRiskInsuredVO>> reinsuranceRiskIdMap = new HashMap<>();
                Map<Long, List<CgnciReinsuranceRiskLayerVO>> reinsuranceRiskInsuredIdMap = new HashMap<>();
                Map<Long, List<CgnciAccessoryVO>> reinsuranceRiskLayerIdMap = new HashMap<>();

                // 分入险种标的
                List<CgnciReinsuranceRiskInsuredDO> divideInRiskInsuredDOList = cgnciReinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                        .in(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskIds));
                if (VerifyUtils.isNotEmpty(divideInRiskInsuredDOList)) {
                    List<CgnciReinsuranceRiskInsuredVO> divideInRiskInsuredVOList = ConvertUtils.convert(divideInRiskInsuredDOList, CgnciReinsuranceRiskInsuredVO.class);
                    // 分入险种标的ID集合
                    List<Long> riskInsuredIds = divideInRiskInsuredVOList.stream().map(CgnciReinsuranceRiskInsuredVO::getRiskInsuredId).collect(Collectors.toList());
                    // 分入险种标的根据分入险种id分组
                    reinsuranceRiskIdMap = divideInRiskInsuredVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskInsuredVO::getReinsuranceRiskId));

                    // 分入险种层
                    List<CgnciReinsuranceRiskLayerDO> divideInRiskLayerDOList = cgnciReinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                            .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, riskInsuredIds));
                    if (VerifyUtils.isNotEmpty(divideInRiskLayerDOList)) {
                        List<CgnciReinsuranceRiskLayerVO> divideInRiskLayerVOList = ConvertUtils.convert(divideInRiskLayerDOList, CgnciReinsuranceRiskLayerVO.class);
                        // ID集合
                        List<Long> riskLayerIds = divideInRiskLayerVOList.stream().map(CgnciReinsuranceRiskLayerVO::getLayerId).collect(Collectors.toList());
                        // 分入险种层根据分入险种标的分组
                        reinsuranceRiskInsuredIdMap = divideInRiskLayerVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskLayerVO::getReinsuranceRiskId));

                        // 查询免赔附件
                        List<CgnciAccessoryDO> accessoryDOList = accessoryService.list(new LambdaQueryWrapper<CgnciAccessoryDO>()
                                .in(CgnciAccessoryDO::getBusinessId, riskLayerIds));
                        if (VerifyUtils.isNotEmpty(accessoryDOList)) {
                            List<CgnciAccessoryVO> accessoryVOList = ConvertUtils.convert(accessoryDOList, CgnciAccessoryVO.class);
                            // 分入险种层根据分入险种标的id分组
                            reinsuranceRiskLayerIdMap = accessoryVOList.stream().collect(Collectors.groupingBy(CgnciAccessoryVO::getBusinessId));
                        }
                    }
                }

                // 组装VO
                Map<Long, List<CgnciReinsuranceRiskInsuredVO>> finalRiskIdMap = reinsuranceRiskIdMap;
                Map<Long, List<CgnciReinsuranceRiskLayerVO>> finalRiskInsuredIdMap = reinsuranceRiskInsuredIdMap;
                Map<Long, List<CgnciAccessoryVO>> finalReinsuranceRiskLayerIdMap = reinsuranceRiskLayerIdMap;
                divideInRiskVOList.forEach(risk -> {
                    if (VerifyUtils.isNotEmpty(finalRiskIdMap)) {
                        List<CgnciReinsuranceRiskInsuredVO> riskInsuredVOS = finalRiskIdMap.get(risk.getReinsuranceRiskId());
                        if (VerifyUtils.isNotEmpty(riskInsuredVOS)) {
                            riskInsuredVOS.forEach(riskInsured -> {
                                if (VerifyUtils.isNotEmpty(finalRiskInsuredIdMap)) {
                                    List<CgnciReinsuranceRiskLayerVO> riskLayerVOS = finalRiskInsuredIdMap.get(riskInsured.getRiskInsuredId());
                                    if (VerifyUtils.isNotEmpty(riskLayerVOS)) {
                                        riskLayerVOS.forEach(riskLayerVO -> {
                                            if (VerifyUtils.isNotEmpty(finalReinsuranceRiskLayerIdMap)) {
                                                List<CgnciAccessoryVO> accessoryVOList = finalReinsuranceRiskLayerIdMap.get(riskLayerVO.getLayerId());
                                                riskLayerVO.setAccessoryVOList(accessoryVOList);
                                            }
                                        });
                                        riskInsured.setRiskLayers(riskLayerVOS);
                                    }
                                }
                            });
                        }
                        risk.setRiskInsureds(riskInsuredVOS);
                    }
                });
                divideInVO.setRisks(divideInRiskVOList);
            }

            // 分入方案
            List<CgnciReinsurerVO> divideInReinsurerVOList = reinsurerService.listDivideInReinsurer(divideInDO.getReinsuranceId());
            divideInVO.setReinsurers(divideInReinsurerVOList);
            return divideInVO;
        }
        return null;
    }

    @Override
    public CgnciReinsuranceInfoVO getLfDivideOutReinsuranceByInsuranceId(Long insuranceId) {
        CgnciReinsuranceInfoDO lfDivideOutDO = this.getOne(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, insuranceId).eq(CgnciReinsuranceInfoDO::getReinsuranceType, 3));
        if (VerifyUtils.isNotEmpty(lfDivideOutDO)) {
            CgnciReinsuranceInfoVO lfDivideOutVO = ConvertUtils.convert(lfDivideOutDO, CgnciReinsuranceInfoVO.class);
            // 分出险种
            List<CgnciReinsuranceRiskDO> lfDivideOutRiskDOList = cgnciReinsuranceRiskService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                    .eq(CgnciReinsuranceRiskDO::getReinsuranceId, lfDivideOutDO.getReinsuranceId()));
            if (VerifyUtils.isNotEmpty(lfDivideOutRiskDOList)) {
                List<CgnciReinsuranceRiskVO> lfDivideOutRiskRiskVOList = ConvertUtils.convert(lfDivideOutRiskDOList, CgnciReinsuranceRiskVO.class);
                List<Long> reinsuranceRiskIds = lfDivideOutRiskRiskVOList.stream().map(CgnciReinsuranceRiskVO::getReinsuranceRiskId).collect(Collectors.toList());

                Map<Long, List<CgnciReinsuranceRiskInsuredVO>> reinsuranceRiskIdMap = new HashMap<>();
                Map<Long, List<CgnciReinsuranceRiskLayerVO>> reinsuranceRiskInsuredIdMap = new HashMap<>();

                // 分出险种标的
                List<CgnciReinsuranceRiskInsuredDO> lfDivideOutRiskInsuredDOList = cgnciReinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                        .in(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskIds));
                if (VerifyUtils.isNotEmpty(lfDivideOutRiskInsuredDOList)) {
                    List<CgnciReinsuranceRiskInsuredVO> lfDivideOutRiskInsuredVOList = ConvertUtils.convert(lfDivideOutRiskInsuredDOList, CgnciReinsuranceRiskInsuredVO.class);
                    // 临分分出险种标的ID集合
                    List<Long> riskInsuredIds = lfDivideOutRiskInsuredVOList.stream().map(CgnciReinsuranceRiskInsuredVO::getRiskInsuredId).collect(Collectors.toList());
                    // 分入险种标的根据分入险种id分组
                    reinsuranceRiskIdMap = lfDivideOutRiskInsuredVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskInsuredVO::getReinsuranceRiskId));

                    // 临分分出险种层
                    List<CgnciReinsuranceRiskLayerDO> lfDivideOutRiskLayerDOList = cgnciReinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                            .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, riskInsuredIds));
                    if (VerifyUtils.isNotEmpty(lfDivideOutRiskLayerDOList)) {
                        List<CgnciReinsuranceRiskLayerVO> lfDivideOutRiskLayerVOList = ConvertUtils.convert(lfDivideOutRiskLayerDOList, CgnciReinsuranceRiskLayerVO.class);
                        // 分入险种层根据分入险种标的分组
                        reinsuranceRiskInsuredIdMap = lfDivideOutRiskLayerVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskLayerVO::getReinsuranceRiskId));
                    }
                }

                // 组装VO
                Map<Long, List<CgnciReinsuranceRiskInsuredVO>> finalReinsuranceRiskIdMap = reinsuranceRiskIdMap;
                Map<Long, List<CgnciReinsuranceRiskLayerVO>> finalReinsuranceRiskInsuredIdMap = reinsuranceRiskInsuredIdMap;
                lfDivideOutRiskRiskVOList.forEach(risk -> {
                    if (VerifyUtils.isNotEmpty(finalReinsuranceRiskIdMap)) {
                        List<CgnciReinsuranceRiskInsuredVO> riskInsuredVOS = finalReinsuranceRiskIdMap.get(risk.getReinsuranceRiskId());
                        if (VerifyUtils.isNotEmpty(riskInsuredVOS)) {
                            riskInsuredVOS.forEach(riskInsured -> {
                                if (VerifyUtils.isNotEmpty(finalReinsuranceRiskInsuredIdMap)) {
                                    List<CgnciReinsuranceRiskLayerVO> riskLayerVOS = finalReinsuranceRiskInsuredIdMap.get(riskInsured.getRiskInsuredId());
                                    riskInsured.setRiskLayers(riskLayerVOS);
                                }
                            });
                            risk.setRiskInsureds(riskInsuredVOS);
                        }
                    }
                });
                lfDivideOutVO.setRisks(lfDivideOutRiskRiskVOList);
            }

            // 再保人
            List<CgnciReinsurerVO> lfDivideOutReinsurerVOList = reinsurerService.listLfDivideOutReinsurer(lfDivideOutDO.getReinsuranceId());
            lfDivideOutVO.setReinsurers(lfDivideOutReinsurerVOList);
            return lfDivideOutVO;
        }
        return null;
    }

    @Override
    public List<CgnciReinsuranceInfoVO> listHyDivideOutReinsuranceByInsuranceId(Long insuranceId) {
        List<CgnciReinsuranceInfoDO> hydivideOutDOList = this.list(new LambdaQueryWrapper<CgnciReinsuranceInfoDO>()
                .eq(CgnciReinsuranceInfoDO::getBusinessId, insuranceId)
                .eq(CgnciReinsuranceInfoDO::getReinsuranceType, 2));
        if (VerifyUtils.isEmpty(hydivideOutDOList)) {
            return null;
        }
        List<CgnciReinsuranceInfoVO> hyDivideOutVOList = ConvertUtils.convert(hydivideOutDOList, CgnciReinsuranceInfoVO.class);
        List<Long> reinsuranceIds = hyDivideOutVOList.stream().map(CgnciReinsuranceInfoVO::getReinsuranceId).collect(Collectors.toList());
        Map<Long, List<CgnciReinsuranceRiskVO>> reinsuranceIdMap = new HashMap<>();
        Map<Long, List<CgnciReinsuranceRiskInsuredVO>> reinsuranceRiskIdMap = new HashMap<>();
        Map<Long, List<CgnciReinsuranceRiskLayerVO>> reinsuranceRiskInsuredIdMap = new HashMap<>();
        Map<Long, List<CgnciCounterpartyVO>> layerIdMap = new HashMap<>();

        // 险种
        List<CgnciReinsuranceRiskDO> hyDivideOutRiskDOList = cgnciReinsuranceRiskService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskDO>()
                .in(CgnciReinsuranceRiskDO::getReinsuranceId, reinsuranceIds));
        if (VerifyUtils.isNotEmpty(hyDivideOutRiskDOList)) {
            List<CgnciReinsuranceRiskVO> reinsuranceRiskVOList = ConvertUtils.convert(hyDivideOutRiskDOList, CgnciReinsuranceRiskVO.class);
            List<Long> reinsuranceRiskIds = reinsuranceRiskVOList.stream().map(CgnciReinsuranceRiskVO::getReinsuranceRiskId).collect(Collectors.toList());
            // 险种根据合约项分组
            reinsuranceIdMap = reinsuranceRiskVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskVO::getReinsuranceId));

            // 合约分出险种标的
            List<CgnciReinsuranceRiskInsuredDO> hyDivideOutRiskInsuredDOList = cgnciReinsuranceRiskInsuredService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskInsuredDO>()
                    .in(CgnciReinsuranceRiskInsuredDO::getReinsuranceRiskId, reinsuranceRiskIds));
            if (VerifyUtils.isNotEmpty(hyDivideOutRiskInsuredDOList)) {
                List<CgnciReinsuranceRiskInsuredVO> hyDivideOutRiskInsuredVOList = ConvertUtils.convert(hyDivideOutRiskInsuredDOList, CgnciReinsuranceRiskInsuredVO.class);
                // 合约分出险种标的ID集合
                List<Long> riskInsuredIds = hyDivideOutRiskInsuredVOList.stream().map(CgnciReinsuranceRiskInsuredVO::getRiskInsuredId).collect(Collectors.toList());
                // 合约分出险种标的根据合约分出险种分组
                reinsuranceRiskIdMap = hyDivideOutRiskInsuredVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskInsuredVO::getReinsuranceRiskId));
                // 合约分出险种层
                List<CgnciReinsuranceRiskLayerDO> hyDivideOutRiskLayerDOList = cgnciReinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                        .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, riskInsuredIds));
                if (VerifyUtils.isNotEmpty(hyDivideOutRiskLayerDOList)) {
                    List<CgnciReinsuranceRiskLayerVO> hyDivideOutRiskLayerVOList = ConvertUtils.convert(hyDivideOutRiskLayerDOList, CgnciReinsuranceRiskLayerVO.class);
                    // 合约分出险种层的ID集合
                    List<Long> layerIds = hyDivideOutRiskLayerVOList.stream().map(CgnciReinsuranceRiskLayerVO::getLayerId).collect(Collectors.toList());
                    // 合约分出险种层根据合约分出险种标的分组
                    reinsuranceRiskInsuredIdMap = hyDivideOutRiskLayerVOList.stream().collect(Collectors.groupingBy(CgnciReinsuranceRiskLayerVO::getReinsuranceRiskId));

                    // 交易对手
                    List<CgnciReinsurerDO> counterpartyDOList = reinsurerService.list(new LambdaQueryWrapper<CgnciReinsurerDO>()
                            .in(CgnciReinsurerDO::getReinsuranceId, layerIds));
                    if (VerifyUtils.isNotEmpty(counterpartyDOList)) {
                        List<CgnciCounterpartyVO> reinsurerVOList = ConvertUtils.convert(counterpartyDOList, CgnciCounterpartyVO.class);
                        // 交易对手根据合约分出险种层分组
                        layerIdMap = reinsurerVOList.stream().collect(Collectors.groupingBy(CgnciCounterpartyVO::getReinsuranceId));
                    }
                }
            }
        }

        // 组装VO
        Map<Long, List<CgnciReinsuranceRiskVO>> finalReinsuranceIdMap = reinsuranceIdMap;
        Map<Long, List<CgnciReinsuranceRiskInsuredVO>> finalReinsuranceRiskIdMap = reinsuranceRiskIdMap;
        Map<Long, List<CgnciReinsuranceRiskLayerVO>> finalReinsuranceRiskInsuredIdMap = reinsuranceRiskInsuredIdMap;
        Map<Long, List<CgnciCounterpartyVO>> finalLayerIdMap = layerIdMap;
        hyDivideOutVOList.forEach(hyDivideOutVO -> {
            if (VerifyUtils.isNotEmpty(finalReinsuranceIdMap)) {
                List<CgnciReinsuranceRiskVO> hyDivideOutRiskRiskVOList = finalReinsuranceIdMap.get(hyDivideOutVO.getReinsuranceId());
                // 分出险种
                if (VerifyUtils.isNotEmpty(hyDivideOutRiskRiskVOList)) {
                    hyDivideOutRiskRiskVOList.forEach(risk -> {
                        if (VerifyUtils.isNotEmpty(finalReinsuranceRiskIdMap)) {
                            List<CgnciReinsuranceRiskInsuredVO> riskInsuredVOS = finalReinsuranceRiskIdMap.get(risk.getReinsuranceRiskId());
                            if (VerifyUtils.isNotEmpty(riskInsuredVOS)) {
                                riskInsuredVOS.forEach(riskInsured -> {
                                    if (VerifyUtils.isNotEmpty(finalReinsuranceRiskInsuredIdMap)) {
                                        List<CgnciReinsuranceRiskLayerVO> riskLayerVOS = finalReinsuranceRiskInsuredIdMap.get(riskInsured.getRiskInsuredId());
                                        if (VerifyUtils.isNotEmpty(riskLayerVOS)) {
                                            riskLayerVOS.forEach(riskLayer -> {
                                                if (VerifyUtils.isNotEmpty(finalLayerIdMap)) {
                                                    List<CgnciCounterpartyVO> counterpartyVOS = finalLayerIdMap.get(riskLayer.getLayerId());
                                                    riskLayer.setCounterpartyVOList(counterpartyVOS);
                                                }
                                            });
                                            riskInsured.setRiskLayers(riskLayerVOS);
                                        }
                                    }
                                });
                                risk.setRiskInsureds(riskInsuredVOS);
                            }
                        }
                    });
                    hyDivideOutVO.setRisks(hyDivideOutRiskRiskVOList);
                }
            }
        });
        return hyDivideOutVOList;
    }

    @Override
    public Integer countHyReinsuranceInfo(Long businessId, String type, String riskSubTypeCode) {
        return this.baseMapper.countHyReinsuranceInfo(businessId, type, riskSubTypeCode);
    }

    @Override
    public void updateReinsuranceColumn(List<CgnciReinsuranceRiskInsuredDO> riskInsuredDOS, CgnciReinsuranceInfoDO reinsuranceInfoDO, Long layerId) {
        // 根据标的id查询险种层
        List<CgnciReinsuranceRiskLayerDO> layers = cgnciReinsuranceRiskLayerService.list(new LambdaQueryWrapper<CgnciReinsuranceRiskLayerDO>()
                .in(CgnciReinsuranceRiskLayerDO::getReinsuranceRiskId, riskInsuredDOS.stream().map(CgnciReinsuranceRiskInsuredDO::getRiskInsuredId).collect(Collectors.toList()))
                .eq(VerifyUtils.isNotEmpty(layerId), CgnciReinsuranceRiskLayerDO::getLayerId, layerId));
        // 分入/分出毛保费
        BigDecimal grossPremium = BeanUtils.sumBigDecimalProperty(layers, CgnciReinsuranceRiskLayerDO::getGrossPremium);
        // 分入/分出手续费
        BigDecimal commission = BeanUtils.sumBigDecimalProperty(layers, CgnciReinsuranceRiskLayerDO::getCommission);
        // 分入/分出净保费
        BigDecimal netPremium = BeanUtils.sumBigDecimalProperty(layers, CgnciReinsuranceRiskLayerDO::getNetPremium);
        // 分入/分出再保经纪费
        BigDecimal brokerFee = BeanUtils.sumBigDecimalProperty(layers, CgnciReinsuranceRiskLayerDO::getBrokerFee);
        // 更新分入/分出信息汇总字段
        if (VerifyUtils.isNotEmpty(reinsuranceInfoDO.getTotalGrossPremium())) {
            reinsuranceInfoDO.setTotalGrossPremium(reinsuranceInfoDO.getTotalGrossPremium().subtract(grossPremium));
        }

        if (VerifyUtils.isNotEmpty(reinsuranceInfoDO.getTotalCommission())) {
            reinsuranceInfoDO.setTotalCommission(reinsuranceInfoDO.getTotalCommission().subtract(commission));
        }

        if (VerifyUtils.isNotEmpty(reinsuranceInfoDO.getTotalNetPremium())) {
            reinsuranceInfoDO.setTotalNetPremium(reinsuranceInfoDO.getTotalNetPremium().subtract(netPremium));
        }

        if (VerifyUtils.isNotEmpty(reinsuranceInfoDO.getTotalBrokerFee())) {
            reinsuranceInfoDO.setTotalBrokerFee(reinsuranceInfoDO.getTotalBrokerFee().subtract(brokerFee));
        }
        this.updateById(reinsuranceInfoDO);
    }

}
